package com.framework.log.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 系统日志切面类
 * 用于处理带有 @Log 注解的方法的日志记录
 *
 * @author zzg
 * @date 2024-12-28
 * @since 1.0
 */
@Configuration
@EnableAsync
@ConditionalOnProperty(prefix = "system.log", name = "enabled", havingValue = "true", matchIfMissing = true)
public class AsyncLogConfig {
    
    @Bean("logExecutor")
    @ConditionalOnProperty(prefix = "system.log", name = "async-enabled", havingValue = "true", matchIfMissing = true)
    public Executor logExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(2);
        // 最大线程数
        executor.setMaxPoolSize(5);
        // 队列容量
        executor.setQueueCapacity(100);
        // 线程名前缀
        executor.setThreadNamePrefix("async-log-");
        // 拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
} 